package com.neusoft.javafeatures9;

import java.util.*;

/**
 * Java Stream dropWhile()用法及代码示例 https://vimsky.com/examples/usage/stream-dropwhile-method-in-java-with-examples.html
 * 包含： stream 各种操作的 示例，都有。
 * TakeWhile & DropWhile in Java 9 Streams with Examples
 */
public class Stream_TakeWhile_DropWhile_Java9_Example {

    public static void main(String[] args) {

        List <Integer> OrderedList = Arrays.asList(1, 3, 5, 7, 9, 13, 16, 17, 19);

        Set <Integer> UnorderedList = Set.of(1, 3, 5, 2, 7);

        Set <Integer> IndependentList = Set.of(25, 75, 100, 125, 150);

        //takeWhile
        //Ordered Stream Example
        System.out.println("takeWhile - Ordered");
        OrderedList.stream()
                .takeWhile(e -> e % 2 != 0)
                .forEach(e -> System.out.println(e));

        //Unordered Stream Example
        System.out.println("\ntakeWhile - Unordered");
        UnorderedList.stream()
                .takeWhile(e -> e % 2 != 0)
                .forEach(System.out::println);

        //Independent Stream Example (When all elements are matching)
        System.out.println("\ntakeWhile - Independent (all elements matching)");
        IndependentList.stream()
                .takeWhile(e -> e % 5 == 0)
                .forEach(System.out::println);

        //Independent Stream Example (When none of the element is matching)
        System.out.println("\ntakeWhile - Independent (none of the element matching)");
        IndependentList.stream()
                .takeWhile(e -> e < 10)
                .forEach(System.out::println);

        // 1, 3, 5, 7, 9, 13, 16, 17, 19
        //dropWhile
        //Ordered Stream Example
        System.out.println("\ndropWhile - Ordered");
        OrderedList.stream()
                .dropWhile(e -> e % 2 != 0)
                .forEach(e -> System.out.println(e));


    }
}
